package com.dove.config.rabbit;

import java.io.IOException;

import com.dove.task.DefAsyncMQTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


@Component
@RabbitListener(queues = "successMqQueue", containerFactory = "rabbitListenerContainerFactory")
public class SuccessMqQueueListener {
	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	DefAsyncMQTask defAsyncMQTask;

	@RabbitHandler
	public void process(@Payload String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, com.rabbitmq.client.Channel channel) throws IOException {
		try {
			logger.info("msg queue get msg = {}", msg);
			defAsyncMQTask.onMessage(msg);
		} catch (Exception e) {
			logger.error("successMqQueue execute error get msg ={}", msg);
		} finally {
			// deliveryTag 代表每次消息的ID，basicAck 告诉mq系统，该消息已经 消费
			channel.basicAck(deliveryTag, false);
		}

	}

}
